import Vue from 'vue'
import App from './App.vue'
import ElementUI from 'element-ui';
import 'element-ui/lib/theme-chalk/index.css';
import axios from "axios";
import VueRouter from 'vue-router';
import router from './router';
import '../src/assets/iconfont.css'
import plugins from '@/components/plugins' // plugins
// 全局方法挂载
import * as echarts from 'echarts';
import 'echarts-gl';
import FormulaInput from "formula-input"


// 首先对拦截器的请求进行设置，并在方法中return config，此处为固定格式
axios.interceptors.request.use(config => {
    var tokenName = window.sessionStorage.getItem('tokenName');    // 从本地缓存读取tokenName值
    var tokenValue = window.sessionStorage.getItem('tokenValue');    // 从本地缓存读取tokenValue值
    var header = {
    };
    if (tokenName != undefined && tokenName != '') {
        header[tokenName] = tokenValue;
    }
    config.headers = header
    return config
})
// 允许同ip地址的前端和后端进程之间进行通信
axios.defaults.withCredentials = true;

// 将 echarts 添加到 Vue.prototype 上以便在组件中直接访问
Vue.prototype.$echarts = echarts;
Vue.prototype.$axios = axios;

const back_port = '9301'
// 部署的时候，修改为部署机器的ip地址
const back_ip2 = '169.2.5.9'
const back_ip = 'localhost'

Vue.prototype.$httpUrl = 'http://'+back_ip+':'+back_port
Vue.prototype.$httpUrlUser = 'http://'+back_ip+':'+back_port+'/user'
Vue.prototype.$httpUrlRecord = 'http://'+back_ip+':'+back_port+'/record'
Vue.prototype.$httpUrlParameter = 'http://'+back_ip+':'+back_port+'/parameter'
Vue.prototype.$httpUrlCategory = 'http://'+back_ip+':'+back_port+'/category'
Vue.prototype.$httpUrlHistoricalData = 'http://'+back_ip+':'+back_port+'/historicalData'
Vue.prototype.$httpUrlVisualization = 'http://'+back_ip+':'+back_port+'/visualization'
Vue.prototype.$httpUrlSchedule = 'http://'+back_ip+':'+back_port+'/schedule'

const python_port = '9303'
// 部署的时候，修改为部署机器的ip地址
// const python_port2 = '5000'
// const python_ip = 'localhost'

Vue.prototype.$Python = 'http://'+back_ip+':'+python_port

Vue.config.productionTip = false
Vue.use(plugins);
Vue.use(VueRouter);
Vue.use(ElementUI);
// 公式编辑器
Vue.use(FormulaInput)


// Vue 实例初始化之前，检查是否有保存的路由状态
const savedRoute = sessionStorage.getItem('savedRoute')

if (savedRoute) {
    const route = JSON.parse(savedRoute)
    router.replace(route.path)
    sessionStorage.removeItem('savedRoute') // 在加载后删除保存的路由状态
}


new Vue({
    router,
    render: h => h(App),
}).$mount('#app')

// 使用beforeunload事件检测页面是否正在刷新
window.addEventListener('beforeunload', () =>{
// 如果页面正在刷新，请保存当前路由状态
    if (performance.navigation.type === 1) {
        sessionStorage.setItem('savedRoute', JSON.stringify(router.app.$route))
    }
})
